--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 主界面，从scene来，用于组织所有的layer，层级等等。




PlayCub = class("PlayCub",function()
    -- 继承
    local ret = {}

    ret.evt = nil


    ret.haveZero = false

    return ret
end)

PlayCub.__index = PlayCub


function PlayCub:create()
    local ret = PlayCub:new()
    return ret
end




--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function PlayCub:bind(node)
    self.node = node
    self.btn = SearchChildByName( self.node, "btn_cub" )
    self.bg_img = SearchChildByName( self.node , "tip" )
    self.fnt_num = SearchChildByName( self.node , "fnt" )

    self.btn:setOpacity(0)
    self:setNum( nil )

    -- self.light = SearchChildByName( self.node , "light" )
    -- local act = cc.CSLoader:createTimeline( "csb/play/cub_lignt.csb" )
    -- self.light:runAction(act)
    -- cclook(" %s" , act~=nil )
    -- act:gotoFrameAndPlay(0, true);

    self.light = SearchChildByName( self.node , "ok" )
    self.light:setVisible(false)
end



function PlayCub:setLinePos(x, y , at )
    self.line_x = x
    self.line_y = y
    self.line_at = at
end



function PlayCub:getLinePos()
    return self.line_x, self.line_y, self.line_at
end


function PlayCub:setIsLeft(b)
    self.isLeft = b
end



function PlayCub:getIsLeft(b)
    return self.isLeft 
end



function PlayCub:isHaveZero(  )
    return self.haveZero
end



function PlayCub:setHaveZero( b )
    self.haveZero = b
    self.light:setVisible(b)
    if(b==false)then 
        self:setShanShuo(false)
    end
end



function PlayCub:setNum(num)
    if(num==0)then
        self.num = num
        self.fnt_num:setString(""..num)
        self.fnt_num:setTextColor( cc.c4b( 250, 231, 4, 255) )
        self.fnt_num:setVisible(true)
        cclog("操 ：设置0了: %d - %d", self.line_x, self.line_y);
        -- 如果是0，就自动消除。
        self:zeroToNilWithAnimation()

    elseif(num~=nil)then
        self.num = num
        self.fnt_num:setString(""..num)
        self.fnt_num:setVisible(true)
        self.fnt_num:setTextColor( cc.c4b( 255, 255, 255, 255) )
        -- 大小晃动特效。
        self:disPlayAction()

    elseif( num==nil ) then
        self.num = num
        self.fnt_num:setVisible(false)
    end
    -- cclook(" 我在[%s-%s] , 设置为:%s ", self.line_x, self.line_y, num )
end


function PlayCub:getNum()
    return self.num
end




function PlayCub:setZeroFunc(evt)
    self.zeroFunc = evt 
end







-- *************************************************************************** --
--      inner
-- *************************************************************************** --
function PlayCub:getIsShow()
    return self.isShow or true
end



function PlayCub:disPlayAction()
    self.fnt_num:setScale( 0.1 )
    local bigs = { 1.3, 0.8, 1.2, 0.86, 1.13, 0.91, 1.07, 0.94, 1.03, 0.97, 1}
    local times = {0.01, 0.01, 0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.03, 0.03, 0.04}
    local acts = {}
    for i=1, 11 do 
        local act = cc.ScaleTo:create( times[i]*3, bigs[i]*2 )
        table.insert( acts, act )
    end
    local seq = cc.Sequence:create(acts)
    self.fnt_num:stopAllActions()
    self.fnt_num:runAction( seq )
end




-- 如果自己是0，自动消失并加分的。
function PlayCub:zeroToNilWithAnimation()
    local angls = { 20, -20, 20, -20, 20, -20, 15, -15, 10, -10, 0}
    local times = {0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0.01}
    local acts = {}
    for i=1, 11 do 
        local act = cc.RotateTo:create( times[i]*2, angls[i] )
        table.insert( acts, act )
    end
    local function endEvt()
        self:setNum(nil)
        if( self.zeroFunc )then
            self.zeroFunc()
        end
        cclog("end func")
        self:setHaveZero(true)
    end 
    table.insert( acts, cc.CallFunc:create( endEvt ) )
    local seq = cc.Sequence:create(acts)
    self.fnt_num:stopAllActions()
    self.fnt_num:runAction( seq )
end



function PlayCub:setShanShuo(b)
    if(b and self.b_shanShuo~=true) then 
        self.b_shanShuo = true
        cclog("闪烁 -- 开始")
        self.light:setVisible(true)
        local fade0 = cc.FadeOut:create(0.2)
        local fade1 = cc.FadeIn:create(0.2)

        local seq = cc.Sequence:create( fade0, fade1 )
        local rep = cc.RepeatForever:create(seq)
        self.light:runAction(rep)
        
    elseif (b==false) then 
        self.b_shanShuo = false
        cclog("闪烁停止")
        self.light:stopAllActions()
        self.light:runAction( cc.FadeIn:create(0.001) )
    end
end



